<?php
/**
 * Created by PhpStorm.
 * User: liubao
 * Date: 2018/9/3
 * Time: 17:11
 */

namespace Home\Controller;


class ContractController extends CommunalController
{
    public function _initialize()
    {
        new LogController();
    }

    public function index()
    {
        parent::check_permission('CRMH-T', '', '');
        $db_contract = M("db_contract");
        $where['del'] = 1;
        $project_id_array = $db_contract->where($where)->field('project_id')->select();
        $project_id_array = array_unique(array_column($project_id_array, 'project_id'));
        $crm_project_array = CrmProjectController::getCrmProjectArray($project_id_array);
        $project_id = I('project_id');
        if (!empty($project_id)) $where['project_id'] = $project_id;
        if (get_permission('CRMH-T', 3, '')) {//个人查看
            $where['_string'] = 'add_uid=' . $this->login_id;
        }
        $contract = $db_contract->where($where)->order('add_date desc')->select();
        $project_ids = $this->column($contract, 'project_id');
        $project_ids = implode(',', $project_ids);
        $db_crm_project = M("db_crm_project");
        $project = $db_crm_project->where("project_id in ($project_ids)")->select();
        $result = array();
        foreach ($contract as $key => $value) {
            foreach ($project as $k => $v) {
                if ($value['project_id'] == $v['project_id']) {
                    $result[] = array_merge($v, $value);
                }
            }
        }
        $count = count($result);
        $Page = new \Think\Page($count, 15);
        $result = array_slice($result, $Page->firstRow, $Page->listRows);
        $show = $Page->show();
        $this->assign('result', $result);
        $this->assign('page', $show);
        $this->assign('project_id', $project_id);
        $this->assign('crm_project_array', $crm_project_array);
        $this->display();
    }

    public function add()
    {
        $userList = get_userName('');
        $add_data = array();
        $db_contract = M("db_contract");
        $db_contract_back = M("db_contract_back");
        $db_crm_project = M("db_crm_project");
        $res_project = $db_crm_project->where("del=1")->select();
        $map['number'] = array('notlike', array('HTO%'));
        $contract_number = $db_contract->field('number')->where($map)->order('number desc')->limit(1)->find();
        $contract_number = !empty($contract_number['number']) ? substr($contract_number['number'], 2) : '000000';
        $number = intval($contract_number);
        $number = 'HT' . str_pad($number + 1, 6, "0", STR_PAD_LEFT);//自增合同编号
        if (($_POST['center_id']) > 0) {
            $add_data['center_id'] = $_POST['center_id'];
            $add_data['project_id'] = $_POST['project_id'];
            $add_data['number'] = $number;
            $add_data['file_name'] = $_POST['file_name'];
            $add_data['file_url'] = '/' . $_POST['file_url'];
            $add_data['contract_money'] = $_POST['contract_money'];
            $add_data['sign_contract_date'] = $_POST['sign_contract_date'];
            $add_data['serve_mouth'] = $_POST['serve_mouth'];
            $add_data['sign_start_date'] = $_POST['sign_start_date'];
            $add_data['sign_end_date'] = $_POST['sign_end_date'];
            $add_data['sign_user_id'] = $_POST['sign_user_id'];
            $add_data['status'] = $_POST['status'];
            $add_data['pay_way'] = $_POST['pay_way'];
            $add_data['back_count'] = $_POST['stages'];
            $add_data['add_uid'] = $_SESSION['dms_uid'];
            $add_data['add_date'] = date('Y-m-d H:i:s');
            try {
                $last_id = $db_contract->add($add_data);
                $add_data_back = array();
                foreach ($_POST['stage_money'] as $k => $v) {
                    if (!empty($v)) {
                        $add_data_back['contract_id'] = $last_id;
                        $add_data_back['project_id'] = $_POST['project_id'];
                        $add_data_back['number'] = $_POST['number'];
                        $add_data_back['stage'] = $k + 1;
                        $add_data_back['stage_money'] = $_POST['stage_money'][$k];
                        $add_data_back['stage_date'] = $_POST['stage_date'][$k];
                        /*$add_data_back['ticket_money'] = $_POST['ticket_money'][$k];*/
                        /*$add_data_back['ticket_date'] = $_POST['ticket_date'][$k];*/
                        $add_data_back['back_status'] = $_POST['back_status'][$k];
                        $add_data_back['type'] = $_POST['type'][$k];
                        $add_data_back['add_uid'] = $_SESSION['dms_uid'];
                        $add_data_back['add_date'] = date('Y-m-d H:i:s');
                        $db_contract_back->add($add_data_back);
                    }
                }
                $this->success("操作成功", __APP__ . "/Contract/index");
                die;
            } catch
            (\Exception $e) {
                $this->error("操作失败", __APP__ . "/Contract/index");
                die;
            }
        }
        $this->assign('user_list', $userList);
        $this->assign('number', $number);
        $this->assign('res_project', $res_project);
        $this->display();

    }

    public function edit()
    {
        $contract_id = $_GET['id'];
        $res_contract = M("db_contract")->where("contract_id=$contract_id")->find();
        $project_id = $res_contract['project_id'];
        $res_project = M("db_project")->where("id=$project_id")->find();
        $res_contract_back = M("db_contract_back")->where("contract_id=$contract_id")->select();
        $res = array_merge($res_project, $res_contract);
        if (!empty($_POST)) {
            $contract_id = $_POST['contract_id'];
            try {
                foreach ($_POST['back_id'] as $k => $v) {
                    $edit['back_id'] = $v;
                    $edit['stage_money'] = $_POST['stage_money'][$k];
                    $edit['stage_date'] = $_POST['stage_date'][$k];
                    /* $edit['ticket_money'] = $_POST['ticket_money'][$k];*/
                    /*$edit['ticket_date'] = $_POST['ticket_date'][$k];*/
                    $edit['back_status'] = $_POST['back_status'][$k];
                    $edit['type'] = $_POST['type'][$k];
                    M("db_contract_back")->save($edit);
                }
                $this->success('修改成功', __APP__ . "/Contract/edit/id/$contract_id");
                die;
            } catch (\Exception $e) {
                $this->add_log('修改收款计划失败', '');
            }
        }
        $this->assign('result', $res);
        $this->assign('res_contract_back', $res_contract_back);
        $this->assign('contract_id', $contract_id);
        $this->assign('count', count($res_contract_back));
        $this->display();
    }

    public function del()
    {
        $id = I('id');
        $db_contract = M("db_contract");
        $db_contract_back = M("db_contract_back");
        $db_contract_invoice = M("db_contract_invoice");
        $db_contract_receipt = M("db_contract_receipt");
        $where['contract_id'] = $id;
        $data['del'] = 0;
        try {
            //收款、发票、收款记录表的关联合同信息全部删除
            $db_contract->where($where)->save($data);
            $db_contract_back->where($where)->save($data);
            $db_contract_invoice->where($where)->save($data);
            $db_contract_receipt->where($where)->save($data);
            $this->success('删除成功', __APP__ . "/Contract/index");
        } catch (\Exception $e) {
            $this->add_log('删除合同和收款计划失败', '');
            $this->error('删除失败', __APP__ . "/Contract/index");
        }
    }

    public function contract_detail()
    {
        $project_id = I('project_id');
        $db_contract = M("db_contract");
        $where['del'] = 1;
        $where['project_id'] = $project_id;
        $res_contract = $db_contract->where($where)->select();
        $this->assign('project_id', $project_id);
        $this->assign('res_contract', $res_contract);
        $this->display();
    }

    public static function getContract($contract_id)
    {
        $where['del'] = 1;
        $res = M("db_contract")->where($where)->where("contract_id=$contract_id")->find();
        if (!empty($res)) {
            return $res;
        }
        return false;
    }

    public static function column(array $array, $columnName)
    {
        if (empty($array)) {
            return array();
        }

        $column = array();

        foreach ($array as $item) {
            if (isset($item[$columnName])) {
                $column[] = $item[$columnName];
            }
        }
        return $column;
    }

}